package com.xiaosu.red_travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaosu.red_travel.bean.ResultInfo;
import com.xiaosu.red_travel.mapper.Comment;
import com.xiaosu.red_travel.mapper.Route;
import com.xiaosu.red_travel.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@CrossOrigin
public class RouteController {

    @Autowired
    private RouteService routeService;

    /**
     * 根据id查询景点
     * @param id 景点id
     * @return 结果对象
     */
    @RequestMapping("/getRouteById")
    public ResultInfo getRouteById(Integer id){
        Route route = routeService.getRouteById(id);
        if(route==null){
            return ResultInfo.no("未找到该景点信息");
        }else {
            return ResultInfo.ok(route);
        }
    }



    /**
     * 模糊查询景点
     * @param name 景点名称
     * @param address 景点地址
     * @param minPrice 最小价格
     * @param maxPrice 最大价格
     * @return 结果对象
     */
    @RequestMapping("/getLikeRote")
    public ResultInfo getLikeRote(@RequestParam(name = "name",defaultValue = "") String name,
                                  @RequestParam(name = "address",defaultValue = "")String address,
                                  @RequestParam(name = "minPrice",defaultValue = "0") Double minPrice,
                                  @RequestParam(name = "maxPrice",defaultValue = "999999999") Double maxPrice){
        QueryWrapper<Route> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        queryWrapper.like("address",address);
        queryWrapper.ge("price",minPrice);
        queryWrapper.le("price",maxPrice);
        List<Route> list = routeService.list(queryWrapper);
        if(list==null || list.size()==0){
            return ResultInfo.no("数据查询失败");
        }else {
            return ResultInfo.ok(list);
        }
    }

    /**
     * 根据地区获取景点信息
     * @param region 地区
     * @return 结果对象
     */
    @RequestMapping("/getRouteByRegion")
    public ResultInfo getRouteByRegion(@RequestParam(name = "region",defaultValue = "") String region){
        QueryWrapper<Route> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("region",region);
        List<Route> list = routeService.list(queryWrapper);
        if(list==null || list.size()==0){
            return ResultInfo.no("数据查询失败");
        }else {
            return ResultInfo.ok(list);
        }
    }

    /**
     * 获取所有景点
     * @return 结果对象
     */
    @RequestMapping("/getAllRoute")
    public ResultInfo getAllRoute(){
        List<Route> list = routeService.getAllRoute();
        if(list==null || list.size()==0){
            return ResultInfo.no("数据查询失败");
        }else {
            return ResultInfo.ok(list);
        }
    }

    /**
     * 获取随机线路
     * @param number 线路条数
     * @return 结果对象
     */
    @RequestMapping("/getRandomRoute")
    public ResultInfo getRandomRoute(@RequestParam(name = "number" ,defaultValue = "7") Integer number){
        List<Route> list = routeService.getRandomRoute(number);
        if(list==null || list.size()==0){
            return ResultInfo.no("获取线路失败");
        }else {
            return ResultInfo.ok(list);
        }
    }

}
